package com.example.mypro;

import java.util.List;

import com.example.adapter.Xw_LvAdapter;
import com.example.domain.XinWen;
import com.example.factory.ServiceFactory;
import com.example.service.InfoService;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class XinWenActivity extends Activity implements OnClickListener,
		OnItemClickListener {
	private ListView listView;
	private ProgressBar pb;
	private InfoService infoService;
	private TextView tv;
	private Button btn;
	private List<XinWen> list = null;
	private GetData getData;
	private Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		infoService = ServiceFactory.getInfoService();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_xinwen);
		listView = (ListView) findViewById(R.id.xw_lv);
		pb = (ProgressBar) findViewById(R.id.xw_pb);
		tv = (TextView) findViewById(R.id.xw_tv);
		btn = (Button) findViewById(R.id.xw_btn);
		tv.setVisibility(View.INVISIBLE);
		btn.setVisibility(View.INVISIBLE);
		btn.setOnClickListener(this);
		listView.setVisibility(View.INVISIBLE);
		listView.setOnItemClickListener(this);
		pb.setVisibility(View.VISIBLE);
		getData = new GetData();
		getData.execute();

	}

	@SuppressLint("ShowToast")
	class GetData extends AsyncTask<Void, Void, List<XinWen>> {

		@SuppressLint("ShowToast")
		@Override
		protected List<XinWen> doInBackground(Void... params) {
			System.out.println("doInBackground");
			list = infoService.getXinWens();
			return list;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);

		}

		@Override
		protected void onPostExecute(List<XinWen> result) {
			super.onPostExecute(result);
			pb.setVisibility(View.INVISIBLE);
//			System.out.println("onProgressUpdate");
			if (list != null) {
				listView.setVisibility(View.VISIBLE);
				Xw_LvAdapter adapter = new Xw_LvAdapter(list,
						XinWenActivity.this);
				listView.setAdapter(adapter);
			} else {
				tv.setVisibility(View.VISIBLE);
				btn.setVisibility(View.VISIBLE);
			}
		}

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.xw_btn) {
			getData = new GetData();
			getData.execute();
		}

	}
 
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		XinWen xinWen = list.get(arg2);
		intent = new Intent(XinWenActivity.this, XinWenActivityItem.class);
		intent.putExtra("xw_bt", xinWen.getXw_bt());
		intent.putExtra("xw_sj", xinWen.getXw_sj());
		intent.putExtra("xw_ly", xinWen.getXw_ly());
		intent.putExtra("xw_nr", xinWen.getXw_nr());
		startActivity(intent);
	}
}
